package com.relytosoft.mizar.media.analyze.services.service.control.impl;


import cn.hutool.core.util.BooleanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.relytosoft.mizar.media.analyze.services.service.control.ControlVerifyService;
import com.relytosoft.mizar.media.common.entity.pojo.dto.AyDispositionPlanDto;
import com.relytosoft.mizar.media.common.entity.pojo.dto.controlPlan.TimeSpanDto;
import com.relytosoft.mizar.media.common.enums.PlanTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import static com.relytosoft.mizar.media.common.constant.ErrorConstants.JSON_PARSE_ERROR;

/**
 * @author wss
 * @version 1.0
 * @date 2022/5/23 17:10
 */
@Service
public class ControlVerifyServiceImpl implements ControlVerifyService {

    protected Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public Boolean controlFrontVerify(AyDispositionPlanDto ayDispositionPlanDto, Long markTime) {
        //验证布防布控状态
        Integer status = ayDispositionPlanDto.getStatus();
        // Integer defenseStatus = ayDispositionPlanDto.getDefenseStatus();
        int trueInt = BooleanUtil.toInt(true);
        return Objects.equals(status, trueInt);
    }

    @Override
    public Boolean controlTemplateVerify(AyDispositionPlanDto ayDispositionPlanDto) {
        int planType = ayDispositionPlanDto.getPlanType();
        PlanTypeEnum planTypeEnum = PlanTypeEnum.getByValue(planType);
        //判断计划类型是否合法
        if (Objects.isNull(planTypeEnum)) {
            return false;
        }


        String planInfoList = ayDispositionPlanDto.getPlanInfoList();
        List<List<TimeSpanDto>> planInfoDtoS;
        try {
            planInfoDtoS = JSON.parseObject(planInfoList, new TypeReference<List<List<TimeSpanDto>>>() {
            });
        } catch (Exception e) {
            log.error("{}--", JSON_PARSE_ERROR, e);
            return false;
        }

        switch (planTypeEnum) {
            case ALL_DAY_PLAN:
                return true;
            case WEEK_PLAN:
            case DAY_PLAN:
                return weekPlanVerify(planInfoDtoS);
            default:
                return false;
        }
    }

    /**
     * 周计划验证
     *
     * @param planInfoDtoS
     * @return
     */
    public Boolean weekPlanVerify(List<List<TimeSpanDto>> planInfoDtoS) {
        int thisDayOfWeek = com.relytosoft.mizar.media.common.utils.DateUtil.thisDayOfWeek();
        //星期一取get(0)
        List<TimeSpanDto> timeSpanDtos = planInfoDtoS.get(thisDayOfWeek - 1);
        //当前计划模板的当前天计划
        if (timeSpanDtos.isEmpty()) {
            return false;
        }
        return dayPlanVerify(timeSpanDtos);
    }

    /**
     * 天计划验证
     *
     * @param timeSpanDtoS
     * @return
     */
    public Boolean dayPlanVerify(List<TimeSpanDto> timeSpanDtoS) {
        if (timeSpanDtoS.isEmpty()) {
            return false;
        }
        Iterator<TimeSpanDto> dtoSIterator = timeSpanDtoS.iterator();
        while (dtoSIterator.hasNext()) {
            TimeSpanDto timeSpanDto = dtoSIterator.next();
            if (timeSpanVerify(timeSpanDto)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 时间段验证
     *
     * @param timeSpanDto
     * @return
     */
    public Boolean timeSpanVerify(TimeSpanDto timeSpanDto) {
        if (Objects.isNull(timeSpanDto)) {
            return false;
        }
        if (Objects.isNull(timeSpanDto.getEndTime()) || Objects.isNull(timeSpanDto.getStartTime())) {
            return false;
        }
        long startTime = com.relytosoft.mizar.media.common.utils.DateUtil.toHourMinute(timeSpanDto.getStartTime());
        long endTime = com.relytosoft.mizar.media.common.utils.DateUtil.toHourMinute(timeSpanDto.getEndTime());
        long nowTime = com.relytosoft.mizar.media.common.utils.DateUtil.getHourMinute();
        if (startTime > nowTime || endTime < nowTime) {
            return false;
        }
        return true;
    }

}
